import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)

const routes = [{
    path: '/',
    redirect: '/login'
  },
  {
    path: "/login",
    component: () => import("../components/login.vue")
  },
  {
    path: '/home',
    component: () => import('../components/home.vue'),
    redirect: '/welcome',
    children: [ {
        path: '/welcome',
        component: () => import('../components/welcome.vue')
      },
      {
        path: '/users',
        component: () => import('../components/user.vue')
      },
      {
        path: '/rights',
        component: () => import('../components/power/rights.vue')
      },
      {
        path: '/roles',
        component: () => import('../components/power/roles.vue')
      },
      {
        path: '/categories',
        component: () => import('../components/goods/categories.vue')
      },
      {
        path: '/params',
        component: () => import('../components/goods/params.vue')
      },
      {
        path: '/goods',
        component: () => import('../components/goods/goods.vue')
      },
      {
        path: '/addGoods',
        component: () => import('../components/goods/addGoods.vue')
      },
      {
        path: '/orders',
        component: () => import('../components/order/orders.vue')
      },
      {
        path: '/reports',
        component: () => import('../components/reports.vue')
      },
    ]
  }

]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  // to:将要访问的路径
  // from:从哪个路径跳转而来
  // next:是一个函数，代表直接放行
  // next() 放行    next('/login')强制跳转
  if (to.path === '/login') return next()
  // 获取token
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  return next()

})
export default router
